/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { AhoCorasickTest } from './ahocorasick.js';
import { ahocorasickData, searchResult } from './ahocorasickData.js';
let runTime = 2000

function new_AhoCorasick_Test() {
    let arr = new Array(10);
    let len = arr.length;
    let start = Date.now();
    for (let i = 0; i < runTime; i++) {
        arr[i % len] = new AhoCorasickTest(['keyword1', 'keyword2', 'etc']);
    }
    let end = Date.now();
    print("ahoCorasick_new_AhoCorasick: " + (end - start)  + ' ms')
}

function search_Test_01() {
    const ahoCorasick = new AhoCorasickTest(['ofo', '960', '$#', 'MWO', '5+7']);
    let result;
    const startTime = Date.now();
    for (let index = 0; index < 200; index++) {
        result = ahoCorasick.search(ahocorasickData);
    }
    const endTime = Date.now();
    if (JSON.stringify(result) != searchResult) {
        throw Error("ahoCorasick_search(string) error");
    }
    print(`ahoCorasick_search(string): ${endTime - startTime} ms`);
}

function search_Test_02() {
    let keys = []
    let words = ""
    for (let i = 0; i < 100; i++) {
        keys.push("keyword" + i)
    }
    for (let i = 0; i < 100; i++) {
        if (i < 99) {
            words += "should find keyword" + i + " at position " + i + "and"
        } else {
            words += "should find keyword" + i + " at position " + i + "."
        }
    }
    let ac = new AhoCorasickTest(keys);
    let arr = new Array(10);
    let len = arr.length;
    let start = Date.now();
    for (let i = 0; i < runTime; i++) {
        arr[i % len] = ac.search(words)
    }
    let end = Date.now();
    print("ahoCorasick_search[100key]: " + (end - start) + ' ms' )
}

new_AhoCorasick_Test();
search_Test_01();
search_Test_02();